package org.acsis.jee.mail;

import java.io.StringWriter;
import java.util.Properties;

import org.acsis.jee.util.ClassLoaderUtil;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class VelocityPrinter {

	private static VelocityEngine ve = null;
	private static String propertiesFile = "velocity";
	
	public static void velocityInit(String properties){
		propertiesFile = properties;
		velocityInit();
	}
	
	private static VelocityEngine velocityInit(){
		ve = new VelocityEngine();
		Properties props = ClassLoaderUtil.loadProperties(propertiesFile);
		

		try	{
			ve.init(props);
		} 
		catch (Exception e)	{
			System.out.println(e.getMessage());
		}
		return ve;
	}

	public static String constructText(VelocityContext ctx, String name, String encoding){
		
		velocityInit();
		
		StringWriter writer = new StringWriter();

		try{
			
			Template t = ve.getTemplate(name + ".vm", encoding);
			t.merge(ctx, writer);
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}

		return writer.toString();
	}

}
